// pages/youthStudy/yourthStudy.js
let videoAd = null
const {
    xxtUrl,
    javaUrl,
    baseUrl
} = require("../../utils/util.js")

Page({

    /**
     * 页面的初始数据
     */
    data: {
        current: 0, //步骤条当前值
        YouthUrl: "", //共青团网址
        mid: "",
        btmDisable: true,
        startBtmDisable: true,
        phoneError: false,
        phoneNumber: '',
        startBtmDisable: true,
        show: false,
        result:[]
    },
    TimeID: -1,

    //步骤条事件
    handleChange({
        detail
    }) {
        console.log(detail.current);

        this.setData({
            current: detail.current,
        });
    },

    //链接输入事件
    inputYouthUrl(e) {
        clearTimeout(this.TimeID);
        this.TimeID = setTimeout(() => {
            console.log(e.detail.value)
            if (e.detail.value !== "") {
                this.setData({
                    current: 1
                })
            }
            this.YouthUrlHandle(e.detail.value)
        }, 1000);
    },

    YouthUrlHandle(url) {
        var pattern = /[1,9]\d{5,}/
        console.log(url.indexOf("memberId"));
        console.log(url.indexOf("&"));
        console.log("YouthUrlHandle", url.slice(url.indexOf("memberId"), url.indexOf("&")))
        const memberId = url.slice(url.indexOf("memberId"), url.indexOf("&"))
        console.log(memberId.match(pattern));
        const mid = memberId.match(pattern)[0]
        this.setData({
            mid: mid,
            btmDisable: false
        })
    },
    //确定mid无误
    btmMid(e) {
        console.log(e)
        this.setData({
            current: 2
        })
    },
    onPhoneInput(e) {
        const {
            phoneError
        } = this.data;
        const isPhoneNumber = /^[1][3,4,5,7,8,9][0-9]{9}$/.test(e.detail.value);
        if (phoneError === isPhoneNumber) {
            this.setData({
                phoneError: !isPhoneNumber,
                startBtmDisable: false,
                phoneNumber: e.detail.value
            });
        } else {
            this.setData({
                startBtmDisable: true
            })
        }
    },

    startBtm(e) {
        console.log(e)
        console.log("mid", this.data.mid)
        this.showPopup()
        if (this.data.mid == "") {
            wx.showToast({
                title: '内容为空',
                duration: 1500,
                icon: "error",
                mask: true,
                success: (res) => {},
                fail: (res) => {},
                complete: (res) => {},
            })
        } else {
            var that = this
            wx.request({
                //https://qwer.lovegcu.xyz/index/?mid=1964808
                url: `https://qwer.lovegcu.xyz/index/?mid=${that.data.mid}`,
                method: "GET",
                data: {
                    "mid": that.data.mid,
                },
                success(res) {
                    console.log(res)
                    if (res.data.code === 200) {
                        wx.showToast({
                            title: '成功',
                            icon: 'success',
                            duration: 3000
                        })

                        that.setData({
                            startBtmDisable: true,
                            mid: "",
                            phoneNumber: "",
                            result:res.data.data,

                        })
                        console.log(res.data.data)
                    }
                }
            })
        }

    },
    LoadVideo(e) {
        console.log(e)
        // 用户触发广告后，显示激励视频广告
        wx.showModal({
            title: '提示',
            content: '看完广告才能获取发送机会',
            success(res) {
                if (res.confirm) {
                    console.log('用户点击确定')
                    if (videoAd) {
                        videoAd.show().catch(() => {
                            // 失败重试
                            videoAd.load()
                                .then(() => videoAd.show())
                                .catch(err => {
                                    console.log('激励视频 广告显示失败')
                                })
                        })
                    }
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        })
    },

    showPopup() {
        this.setData({
            show: true
        });
    },
    onClose() {
        this.setData({
            show: false
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        // 在页面onLoad回调事件中创建激励视频广告实例
        if (wx.createRewardedVideoAd) {
            videoAd = wx.createRewardedVideoAd({
                adUnitId: 'adunit-5aad7555515c9fcd'
            })
            videoAd.onLoad(() => {})
            videoAd.onError((err) => {})
            videoAd.onClose((res) => {
                if (res.isEnded) {
                    this.setData({
                        startBtmDisable: false
                    })
                } else {
                    wx.showToast({
                        title: '未看完广告',
                    })
                }
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})